const dbUtils = require('../utils/db-util');

const message = {

	/**
	 * 获取用户所有消息
	 * @param  {object} model 消息数据模型
	 * @return {object}       mysql执行结果
	 */
	async getAllMessage(model) {
		// 按照最近时间排序
		let _sql = `
			select a.*, b.user_name from (select * from message_user order by time desc) a 
				join user_info b on a.to_user = ${model.to_user} where a.from_user = b.id
				group by a.from_user`;
		let result = await dbUtils.query(_sql);
		if(Array.isArray(result) && result.length > 0) {
			result = result;
		} else {
			result = null;
		}
		return result;
	},
};

module.exports = message;